﻿using System;
using System.Threading.Tasks;

using Linq2Crm.ObjectModel.DomainObject;

namespace Linq2Crm.Request
{
	public class DeleteRequest : RequestBase
	{
		#region DeleteType enum
		public enum DeleteType
		{
			ChangeStatus,
			Delete
		}
		#endregion

		protected DeleteRequest(Type type) : base(type)
		{
		}

		public DeleteType DeleteMethod { get; set; }

		public IDomainObject Entity { get; set; }

		public Action<DeleteResponse> Callback { get; set; }

		public static DeleteRequest Create<T>(IDomainObject entity, DeleteType type = DeleteType.Delete)
			where T : IDomainObject
		{
         if (entity == null)
         {
            throw new ArgumentNullException();
         }

         return new DeleteRequest(entity.GetType())
			       	{
			       		DeleteMethod = type,
			       		Entity = entity
			       	};
		}

		internal override void Execute()
		{
			DomainObjectContext.Instance.ExecuteDeleteRequest(this);
		}

		internal override async Task<ResponseBase> ExecuteAsync()
		{
			var response = await DomainObjectContext.Instance.ExecuteDeleteRequestAsync(this);
			return response;
		}
	}
}